import {getProjects} from '@/api/project'
import { Notification } from 'element-ui'
import {getDocInfoById, getDocLogs, getDocs} from '@/api/document'

const currentProjectKey = 'currentProject'
const currentTodoKey = 'currentTodo'
// const currentMenuKey = 'currentMenu'
const currentProjectCreatorKey = 'currentProjectCreatorKey'
const current = {
  state: {
    currentProject: localStorage.getItem(currentProjectKey) ? parseInt(localStorage.getItem(currentProjectKey)) : undefined,
    currentTodo: localStorage.getItem(currentTodoKey) ? parseInt(localStorage.getItem(currentTodoKey)) : undefined,
    currentMenu: undefined,
    currentProjectCreator: localStorage.getItem(currentProjectCreatorKey) ? parseInt(localStorage.getItem(currentProjectCreatorKey)) : undefined,
    currentProjectData: undefined,
    currentUserData: undefined,
    currentProjectTodos: undefined,
    isTokenUnExpire: false,
    projects: [],
    documents: [],
    documentLogs: [],
    currentDoc: undefined,
    currentDocInfo: undefined
  },
  mutations: {
    SET_CURRENT_PROJECT: (state, projectId) => {
      state.currentProject = projectId
      if (projectId) localStorage.setItem(currentProjectKey, state.currentProject + '')
    },
    SET_CURRENT_TODO: (state, todoId) => {
      state.currentProject = todoId
      if (todoId) localStorage.setItem(currentTodoKey, state.currentTodo + '')
    },
    SET_CURRENT_MENU: (state, menuIndex) => {
      state.currentMenu = menuIndex
      // if (menuIndex) localStorage.setItem(currentMenuKey, state.currentMenu)
    },
    SET_CURRENT_PROJECT_CREATOR: (state, userId) => {
      state.currentProjectCreator = userId
      if (userId) localStorage.setItem(currentProjectCreatorKey, state.currentProjectCreator + '')
    },
    SET_CURRENT_PROJECT_DATA: (state, project) => {
      state.currentProjectData = project
    },
    SET_CURRENT_USER_DATA: (state, userData) => {
      state.currentUserData = userData
    },
    SET_CURRENT_PROJECT_TODOS: (state, todos) => {
      state.currentProjectTodos = todos
    },
    SET_IS_LOGGED: (state, status = false) => {
      state.isTokenUnExpire = status
    },
    SET_PROJECTS: (state, projects) => {
      state.projects = projects
    },
    SET_DOCUMENTS: (state, documents) => {
      state.documents = documents
    },
    SET_DOCUMENT_LOGS: (state, logs) => {
      if (!logs) {
        state.documentLogs = []
      } else {
        state.documentLogs = logs
      }
    },
    SET_CURRENT_DOC: (state, docId) => {
      state.currentDoc = docId
    },
    SET_CURRENT_DOC_INFO: (state, docInfo) => {
      state.currentDocInfo = docInfo
    }
  },
  actions: {
    SetCurrentProject ({ commit }, projectId) {
      return new Promise(resolve => {
        if (projectId === 'undefined') {
          commit('SET_CURRENT_PROJECT', undefined)
        } else {
          commit('SET_CURRENT_PROJECT', projectId)
        }
        resolve()
      })
    },
    SetCurrentTodo ({ commit }, todoId) {
      return new Promise(resolve => {
        commit('SET_CURRENT_TODO', todoId)
        resolve()
      })
    },
    SetCurrentMenu ({ commit }, menuIndex) {
      return new Promise(resolve => {
        commit('SET_CURRENT_MENU', menuIndex)
        resolve()
      })
    },
    SetCurrentProjectCreator ({ commit }, userId) {
      return new Promise(resolve => {
        commit('SET_CURRENT_PROJECT_CREATOR', userId)
        resolve()
      })
    },
    LoadProjects ({commit}) {
      return new Promise(resolve => {
        getProjects().then(res => {
          commit('SET_PROJECTS', res.data)
        }).catch(error => {
          Notification.error(error)
        })
        resolve()
      })
    },
    LoadDocuments ({commit}, projectId) {
      return new Promise(resolve => {
        getDocs(projectId).then(res => {
          commit('SET_DOCUMENTS', res.data)
        }).catch(error => {
          Notification.error(error)
        })
        resolve()
      })
    },
    LoadDocumentLogs ({commit}, docId) {
      return new Promise(resolve => {
        getDocLogs(docId).then(res => {
          commit('SET_DOCUMENT_LOGS', res.data)
        }).catch(error => {
          Notification.error(error)
        })
        resolve()
      })
    },
    LoadDocumentInfo ({ commit }, projectId, docId) {
      return new Promise(resolve => {
        getDocInfoById(projectId, docId).then(res => {
          commit('SET_CURRENT_DOC_INFO', res.data)
          commit('SET_CURRENT_DOC', docId)
        }).catch(error => {
          Notification.error(error)
        })
        resolve()
      })
    },
    refreshAccessToken ({ commit }) {

    },
    ClearCache ({ commit }) {
      return new Promise(resolve => {
        commit('SET_CURRENT_PROJECT', undefined)
        commit('SET_CURRENT_TODO', undefined)
        commit('SET_CURRENT_MENU', undefined)
        commit('SET_CURRENT_PROJECT_CREATOR', undefined)
        commit('SET_CURRENT_PROJECT_DATA', undefined)
        commit('SET_CURRENT_USER_DATA', undefined)
        commit('SET_CURRENT_PROJECT_TODOS', undefined)
        commit('SET_PROJECTS', [])
        commit('SET_DOCUMENTS', [])
        commit('SET_DOCUMENT_LOGS', [])
        commit('SET_CURRENT_DOC', undefined)
        commit('SET_TOKEN', '')
        commit('SET_TOKEN_EXPIRED', undefined)
        commit('SET_REFRESH_TOKEN', '')
        resolve()
      })
    }
  }
}

export default current
